package de.schildbach.wallet.menu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import com.mypgwali.R;
import de.schildbach.wallet.WalletApplication;
import de.schildbach.wallet.ui.SendCoinsFragment;
import de.schildbach.wallet.ui.WalletBalanceFragment;

public class BaseActivity extends SlidingFragmentActivity {

	private int mTitleRes;
	protected Fragment mFrag;
	protected Fragment mContent;

	protected SlidingMenu sm;

	// //public BaseActivity(int titleRes) {
	// mTitleRes = titleRes;
	// }
	
	
	private static View walletContent;
	private View contentFrame;

	public void switchContent(Fragment fragment, String title, boolean wallet) {
		mContent = fragment;

		setTitle(title);

//		int optionId = wallet ? R.layout.wallet_content
//				: R.layout.content_frame;
//
//		View C = findViewById(android.R.id.content);
//		ViewGroup parent = (ViewGroup) C.getParent();
//		int index = parent.indexOfChild(C);
//		parent.removeView(C);
//		C = getLayoutInflater().inflate(optionId, parent, false);
//		parent.addView(C, index);

		if (wallet) {
			
			setContentView(walletContent);
//			setContentView(R.layout.wallet_content);
//			
//			getSlidingMenu().toggle();
		} else {
			
//			TextView t1 = new TextView(this);
//			t1.setText("no wallet");
//			setContentView(t1);
			
			
			setContentView(R.layout.content_frame);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, fragment).commit();
//			getSlidingMenu().showContent();
		}
		getSlidingMenu().showContent();

	}

	@Override
	public void onBackPressed() {
		if (sm.isMenuShowing()) {
			sm.showContent();
		} else {
			super.onBackPressed();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		WalletApplication app = (WalletApplication) getApplication();
		
		setBehindContentView(R.layout.menu_frame);
		
		if (walletContent == null) {
			walletContent = getLayoutInflater().inflate( R.layout.wallet_content, null);

			setContentView(walletContent);
			
			if (savedInstanceState == null) {
				FragmentTransaction t = this.getSupportFragmentManager()
						.beginTransaction();
				mFrag = new WalletBalanceFragment();
				t.replace(R.id.menu_frame, mFrag);
				t.commit();
			} else {
				mFrag = (Fragment) this.getSupportFragmentManager()
						.findFragmentById(R.id.menu_frame);
			}

			sm = getSlidingMenu();
			sm.setShadowWidthRes(R.dimen.shadow_width);
			sm.setShadowDrawable(R.drawable.shadow);
			sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
			sm.setFadeDegree(0.35f);
			sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
		
	}

	// @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home: {

			return true;
		}
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

}
